package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.common.CommonReslut;
import com.neusoft.entity.SystemMenu;
import com.neusoft.service.SystemMenuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * 系统菜单表(SystemMenu)表控制层
 *
 * @author makejava
 * @since 2020-05-27 17:27:48
 */
@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {
    /**
     * 服务对象
     */
    @Resource
    private SystemMenuService systemMenuService;




    //主界面菜单的查询
    @GetMapping("queryAll")
    @ResponseBody
    public Map<String, Object> queryAll() {
        return this.systemMenuService.queryAll();
    }

    //显示菜单管理的界面
    //管理查询界面
    @RequestMapping("tolist")
    public String tolist() {
        return "/function/table";
    }

    //菜单管理页面的查询所有功能
    @PostMapping("queryAllIn")
    @ResponseBody
    public Object queryAllIn(@RequestParam("page") Integer page, @RequestParam("limit")Integer limit, @RequestBody SystemMenu systemMenu){
        CommonReslut<SystemMenu> result=new CommonReslut<>();
        IPage<SystemMenu> iPage =systemMenuService.queryAllByLimit(page,limit,systemMenu);
        result.setCode(0);
//        System.out.println("iPage.getTotal():::"+iPage.getTotal());
        result.setCount(iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }

    @RequestMapping("toadd")
    public String toadd(Model model) {
        model.addAttribute("systemMenu",new SystemMenu());
        return "/function/menu_add";
    }
    //管理添加保存功能
    @PostMapping("save")
    @ResponseBody
    public Object save(@RequestBody SystemMenu systemMenu) {
        boolean result=false;

        if(systemMenu.getId()!=null){
            //编辑
            result =systemMenuService.update(systemMenu)>0;
        }else{
            //添加
            result=systemMenuService.insert(systemMenu).getId()!=null;
        }


        return result;
    }
    //管理更新功能
    @RequestMapping("toedit")
    public String toedit(int id,Model model) {

        System.out.println("id:::"+id);
        SystemMenu systemMenu=systemMenuService.queryById(id);
        model.addAttribute("systemMenu",systemMenu);

        return "function/menu_add";
    }
    //管理删除功能
    @PostMapping("delete")
    @ResponseBody
    public boolean delete(@RequestBody Integer[] ids) {

        if(ids==null ||ids.length==0)
            return false;
//        System.out.println(ids[0]);
//        return true;

        return systemMenuService.deleteById(Arrays.asList(ids));
}
}